/*
 * CodeSaveCommand.java
 *
 * Created on 2006년 3월 29일 (수), 오후 4:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.qbooks.publisher;

import java.io.File;
import java.util.*;




import javax.servlet.*;
import javax.servlet.http.*;

import org.json.simple.JSONObject;

import com.zll.framework.conf.Configuration;
import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;
import com.zll.framework.servlet.Command;
import com.qbooks.common.CommonDAO;
import com.qbooks.common.CommonUtil;





/**
 *
 * @author eastar
 */
public class PublisherRegistCommand implements Command
{

    public String execute(HttpServletRequest  request, HttpServletResponse response)
    throws Exception
    {
    	JSONObject json = new JSONObject();

		HashBox box = new HashBox(request);

		PublisherDAO dao 		= new PublisherDAO();

		
		HashList list = dao.selectPublisherList(box.getParam());
		
		
		HashMap resultMap		= new HashMap();
		
		
		if (list.size() == 1) {
			resultMap.put("result", "false");
			resultMap.put("message", "사용중인 아이디입니다.");
		} 
		else 
		{
			String docPath 	= File.separator + box.get("publisher_id") + File.separator + "doc";
			File dir = new File(Configuration.get("dir.storage") + docPath);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			
			//사업자등록증 사본
			File f = box.getFile("biz_no_path");
			if (f != null) {
				File newFile = new File(dir.getPath() + File.separator + box.get("publisher_id") + "_biz" + f.getName().substring(f.getName().lastIndexOf('.')).toLowerCase());
				f.renameTo(newFile);
				box.put("biz_no_path", docPath + File.separator + newFile.getName());
			}
				
			//통신판매신고증 사본
			f = box.getFile("sales_no_path");
			if (f != null) {
				File newFile = new File(dir.getPath() + File.separator + box.get("publisher_id") + "_sales" + f.getName().substring(f.getName().lastIndexOf('.')).toLowerCase());
				f.renameTo(newFile);
				box.put("sales_no_path", docPath + File.separator + newFile.getName());
			}
			
			//통장 사본
			f = box.getFile("account_no_path");
			if (f != null) {
				File newFile = new File(dir.getPath() + File.separator + box.get("publisher_id") + "_acount" + f.getName().substring(f.getName().lastIndexOf('.')).toLowerCase());
				f.renameTo(newFile);
				box.put("account_no_path", docPath + File.separator + newFile.getName());
			}
			
			
			dao.savePublisher(box);
			
			resultMap.put("result", "true");
			resultMap.put("action", "./registSuccess.jsp?publisher_id=" + box.get("publisher_id"));
		}

		

    	request.setAttribute("result", resultMap);

    	return "/common/move_view.jsp";
    }
}
